package org.ibase4j.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.ibase4j.core.base.BaseModel;
import org.ibase4j.core.base.BaseService;
import org.ibase4j.core.util.InstanceUtil;
import org.ibase4j.mapper.PlatProMenuMapper;
import org.ibase4j.model.PlatProDic;
import org.ibase4j.model.PlatProMenu;

/**
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:19:19
 */
@Service
@CacheConfig(cacheNames = "PlatMenu")
public class PlatProMenuService extends BaseService<PlatProMenu> {
	@Autowired
	private PlatProDicService platProDicService;

	public PlatProMenu queryById(Long id) {
		PlatProMenu platProMenu = super.queryById(id);
		if (platProMenu != null) {
			if (platProMenu.getParentId() != null && platProMenu.getParentId() != 0) {
				PlatProMenu parent = super.queryById(platProMenu.getParentId());
				if (parent != null) {
					platProMenu.setParentName(parent.getMenuName());
				} else {
					platProMenu.setParentId(null);
				}
			}
		}
		return platProMenu;
	}

	public List<PlatProMenu> queryList(Map<String, Object> params) {
		params.put("orderBy", "parent_id,sort_no");
		List<PlatProMenu> pageInfo = super.queryList(params);
		Map<String, String> menuTypeMap = platProDicService.queryDicByType("MENUTYPE");
		Map<Long, Integer> leafMap = InstanceUtil.newHashMap();
		List<PlatProMenu> resultList = InstanceUtil.newArrayList();
		Map<Long, List<PlatProMenu>> map = InstanceUtil.newHashMap();
		for (PlatProMenu platProMenu : pageInfo) {
			if (platProMenu != null) {
				if (platProMenu.getMenuType() != null) {
					platProMenu.setTypeName(menuTypeMap.get(platProMenu.getMenuType().toString()));
				}
				if (leafMap.get(platProMenu.getParentId()) == null) {
					leafMap.put(platProMenu.getParentId(), 0);
				}
				leafMap.put(platProMenu.getParentId(), leafMap.get(platProMenu.getParentId()) + 1);
				if (map.get(platProMenu.getParentId()) == null) {
					map.put(platProMenu.getParentId(), new ArrayList<PlatProMenu>());
				}
				map.get(platProMenu.getParentId()).add(platProMenu);
				if (platProMenu.getParentId() == null || platProMenu.getParentId() == 0) {
					resultList.add(platProMenu);
				}
			}
		}
		for (PlatProMenu platProMenu : pageInfo) {
			if (platProMenu != null) {
				if (leafMap.get(platProMenu.getId()) != null && leafMap.get(platProMenu.getId()) > 0) {
					platProMenu.setLeaf(0);
				}
				if (map.get(platProMenu.getId()) != null) {
					resultList.addAll(resultList.indexOf(platProMenu) + 1, map.get(platProMenu.getId()));
				}
			}
		}

		return resultList;
	}

	public List<Object> queryTreeList(Map<String, Object> params) {
		params.put("orderBy", "parent_id,sort_no");
		List<PlatProMenu> pageInfo = super.queryList(params);
		Map<String, String> menuTypeMap = platProDicService.queryDicByType("MENUTYPE");
		Map<String, Object> dicParam = InstanceUtil.newHashMap();
		dicParam.put("type", "CRUD");
		List<PlatProDic> platProDics = platProDicService.queryList(dicParam);
		Map<Long, Integer> leafMap = InstanceUtil.newHashMap();
		List<Object> resultList = InstanceUtil.newArrayList();
		Map<Long, List<PlatProMenu>> map = InstanceUtil.newHashMap();
		for (PlatProMenu platProMenu : pageInfo) {
			if (platProMenu != null) {
				if (platProMenu.getMenuType() != null) {
					platProMenu.setTypeName(menuTypeMap.get(platProMenu.getMenuType().toString()));
				}
				if (leafMap.get(platProMenu.getParentId()) == null) {
					leafMap.put(platProMenu.getParentId(), 0);
				}
				leafMap.put(platProMenu.getParentId(), leafMap.get(platProMenu.getParentId()) + 1);
				if (map.get(platProMenu.getParentId()) == null) {
					map.put(platProMenu.getParentId(), new ArrayList<PlatProMenu>());
				}
				map.get(platProMenu.getParentId()).add(platProMenu);
				if (platProMenu.getParentId() == null || platProMenu.getParentId() == 0) {
					resultList.add(platProMenu);
				}
			}
		}
		for (PlatProMenu platProMenu : pageInfo) {
			if (platProMenu != null) {
				if (map.get(platProMenu.getId()) != null) {
					resultList.addAll(resultList.indexOf(platProMenu) + 1, map.get(platProMenu.getId()));
				}
			}
		}
		for (PlatProMenu platProMenu : pageInfo) {
			if (platProMenu != null) {
				if (leafMap.get(platProMenu.getId()) != null && leafMap.get(platProMenu.getId()) > 0) {
					platProMenu.setLeaf(0);
				} else {
					List<Map<String, Object>> dicMaps = InstanceUtil.newArrayList();
					for (PlatProDic platProDic : platProDics) {
						if (!"read".equals(platProDic.getCode())) {
							Map<String, Object> dicMap = InstanceUtil.transBean2Map(platProDic);
							dicMap.put("id", "D" + platProDic.getId());
							dicMap.put("menuName", platProDic.getCodeText());
							dicMap.put("parentId", platProMenu.getId().toString());
							dicMaps.add(dicMap);
						}
					}
					resultList.addAll(resultList.indexOf(platProMenu) + 1, dicMaps);
				}
			}
		}
		return resultList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ibase4j.provider.SysMenuProvider#getPermissions()
	 */
	public List<Map<String, String>> getPermissions(BaseModel model) {
		return ((PlatProMenuMapper) mapper).getPermissions();
	}

}
